package cn.buildoncloud.labor.workermanapplet.business.modules.controller;

import cn.buildoncloud.labor.workermanapplet.business.modules.services.FeedbackFormService;
import cn.buildoncloud.labor.workermanapplet.common.base.Result;
import cn.buildoncloud.labor.workermanapplet.util.HttpUtil;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/feedbackForm")
public class FeedbackFormController {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FeedbackFormService feedbackFormService;

    @Value("${webSystemIP}")
    private String webSystemIP;

    /**
     * 发送反馈信息
     * @param userId
     * @param content
     * @return
     */
    @PostMapping("/submitFeedbackForm")
    public Object submitFeedbackForm(
            @RequestParam String userId,
            @RequestParam String content,
            @RequestParam String[] imagePath){
        try{

            Map<String, Object> parameterMap = new HashMap<>();
            parameterMap.put("filePath", Arrays.toString(imagePath));
            String uploadResult = HttpUtil.post(webSystemIP + "/fileupload/baseUploadFiles", parameterMap, 1000 * 60);
            log.error(uploadResult);

            Result result = JSON.parseObject(uploadResult, Result.class);
            List<Map<String, Object>> datas = (List<Map<String, Object>>) result.getData();

            String[] imageIds = new String[datas.size()];
            for (int i = 0; i < imageIds.length; i++) {
                imageIds[i] = datas.get(0).get("id").toString();
            }

            feedbackFormService.submitFeedbackForm(Long.parseLong(userId), content, imageIds);

            return Result.success("信息发送成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return Result.error("信息发送失败");
        }
    }

}
